package com.softstao.softstaolibrary.library.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.softstao.softstaolibrary.R;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/23.
 */

public class PhotoListActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private ImageView[] tips;
    private List<String> images = new ArrayList<>();
    private int currentPage=0;//当前展示的页码
    private float mScale;
    @Override

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_list);
        viewPager = (ViewPager)findViewById(R.id.view_pager);
        LinearLayout tipsBox = (LinearLayout)findViewById(R.id.viewGroup);
        ImageView back = (ImageView) findViewById(R.id.back_btn);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        images = getIntent().getStringArrayListExtra("urls");
        tips = new ImageView[images.size()];
        for(int i=0;i<tips.length;i++){
            ImageView img = new ImageView(this);
            mScale = this.getResources().getDisplayMetrics().density;
            int imageParams = (int) (mScale * 10 + 0.5f);// XP与DP转换，适应不同分辨率
            int imagePadding = (int) (mScale * 5 + 0.5f);
            LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(imageParams,imageParams);
            layout.setMargins(3, 0, 3, 0);
            img.setLayoutParams(layout);
            img.setPadding(imagePadding, imagePadding, imagePadding, imagePadding);

            tips[i]=img;

            if(i==0) {
                tips[i].setBackgroundResource(R.mipmap.page_indicator_focused);
            } else{
                tips[i].setBackgroundResource(R.mipmap.page_indicator_unfocused);
            }
            tipsBox.addView(img);
        }

        PagerAdapter adapter=new PagerAdapter(){

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return images.size();
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0==arg1;
            }

            @Override
            public void destroyItem(ViewGroup container,int position,Object o){
                //container.removeViewAt(position);
            }

            @Override
            public Object instantiateItem(ViewGroup container,int position){
                ImageView im=new ImageView(PhotoListActivity.this);
                Glide.with(PhotoListActivity.this).load(images.get(position)).apply(new RequestOptions().fitCenter().error(R.mipmap.img_error).diskCacheStrategy(DiskCacheStrategy.ALL)).into(im);
                container.addView(im);
                return im;
            }
        };
        viewPager.setAdapter(adapter);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageSelected(int position) {
                // TODO Auto-generated method stub
                tips[currentPage].setBackgroundResource(R.mipmap.page_indicator_unfocused);
                currentPage=position;
                tips[position].setBackgroundResource(R.mipmap.page_indicator_focused);
            }

        });

        setMiuiStatusBarDarkMode(true);
    }

    public boolean setMiuiStatusBarDarkMode(boolean darkmode) {
        Class<? extends Window> clazz = getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
